package com.farewell.blog.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.farewell.blog.entity.QueryBean;
import com.farewell.blog.pojo.Blog;
import com.farewell.blog.pojo.Type;
import com.farewell.blog.service.BlogService;
import com.farewell.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Farewell
 * @date 2021/2/15
 */
@Controller
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String type(Model model, @PathVariable("id") Long id, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        List<Type> types = typeService.listType(new QueryBean(1, 20, null)).getRecords();
        QueryBean queryBean = new QueryBean(pageNum, 10, id+"");
        Page<Blog> page = blogService.listBlogByTypeId(queryBean);
        model.addAttribute("types", types);
        model.addAttribute("page", page);
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
